package map03;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * HashMap的第二种遍历方式:利用entrySet方法遍历
 *  public Set<Map.Entry<K,V>> entrySet():
 *    将HashMap的每个键值对封装成一个Node<K,V>对象(Node类的父接口是Map.Entry),再把Node<K,V>对象存到Set集合中
 */
public class HashMapDemo01 {
    public static void main(String[] args) {
        //method01();
        HashMap<String, String> hm = new HashMap<>();
        hm.put("无忌", "赵敏");
        hm.put("乔峰", "阿朱");
        hm.put("段誉", "王语嫣");

        for (Map.Entry<String, String> node : hm.entrySet()) {
            System.out.println(node.getKey() + " " + node.getValue());
        }
    }

    private static void method01() {
        HashMap<String, String> hm = new HashMap<>();
        hm.put("无忌", "赵敏");
        hm.put("乔峰", "阿朱");
        hm.put("段誉", "王语嫣");

        Set<Map.Entry<String, String>> entries = hm.entrySet();
        for (Iterator<Map.Entry<String, String>> iterator = entries.iterator(); iterator.hasNext(); ) {
            Map.Entry<String, String> node = iterator.next();
            String key = node.getKey();
            String value = node.getValue();
            System.out.println(key + " " + value);
        }
    }
}
